package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.pojo.Brand;
import com.spzx.product.pojo.Category;
import com.spzx.product.pojo.CategoryBrand;
import com.spzx.product.pojo.ProductUnit;
import com.spzx.product.service.ICategoryBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-03-12
 */
@Tag(name = "分类品牌")
@RestController
@RequestMapping("/categoryBrand")
public class CategoryBrandController extends BaseController {
    @Autowired
    private ICategoryBrandService categoryBrandService;

    /**
     * 获取商品单位列表
     * @param name 分类品牌名称
     * @return getDataTable(list) 分类品牌列表
     */
    @Operation(summary = "获取分类品牌列表")
    // @RequiresLogin  // 设置访问权限：登入
    // @RequiresRoles("admin")  // 设置访问权限：用户角色
    @RequiresPermissions("product:categoryBrand:list")  // 设置访问权限：权限字符
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(name = "name", description = "分类品牌名称") String name) {
        // 分页
        startPage();

        List<CategoryBrand> list = categoryBrandService.selectCategoryBrandList(name);
        return getDataTable(list);
    }

    @Operation(summary = "新增分类品牌")
    @RequiresPermissions("product:categoryBrand:add")
    @Log(title = "分类品牌管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(/*@Validated*/ @Parameter(description = "分类品牌") @RequestBody CategoryBrand categoryBrand)
    {
        /*
        // 已在 insertCategoryBrand()方法中进行验证，无需单独验证
        if (!categoryBrandService.checkCategoryBrandNameUnique(categoryBrand))
        {
            return error("新增分类'" + categoryBrand.getCategoryName() + "'下品牌'" + categoryBrand.getBrandName() + "'失败，该分类下该品牌名称已存在");
        }
        */
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(categoryBrandService.insertCategoryBrand(categoryBrand));
    }

    @Operation(summary = "删除分类品牌")
    @RequiresPermissions("product:categoryBrand:remove")
    @Log(title = "分类品牌管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@Parameter(name = "ids", description = "分类品牌ids") @PathVariable List<Long> ids) {
        return toAjax(categoryBrandService.removeBatchByIds(ids));
    }


    @Operation(summary = "根据分类id获取品牌列表")
    @GetMapping("/brandList/{categoryId}")
    public AjaxResult selectBrandListByCategoryId(
            @Parameter(description = "分类id")
            @PathVariable Long categoryId) {
        return success(categoryBrandService.selectBrandListByCategoryId(categoryId));
    }

}
